//
//  HSReplayListView.swift
//  Home_School_08
//
//  Created by 张晓林 on 2016/12/30.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit

class HSReplayListView: UIView {

    @IBOutlet weak var likeListL: UILabel!
    @IBOutlet weak var likeLineV: UIView!
    // 记录当前已经添加的标签视图
    var replayListLArray: [UILabel] = []
    
    // 接收消息模型的属性
    var messageVM: HSMessageViewModel? {
        didSet {
            // 默认在添加回复列表视图的时候，需要先清除cell上面原有的标签
            for L in replayListLArray {
                // 1.从父视图中先移除
                L.removeFromSuperview()
            }
            // 2.再清空数组
            replayListLArray.removeAll()
            
            // 获得点赞人数
            let likeNum = messageVM?.message.likenum ?? 0
            
//            likeListL.text = message?.likelist
            likeListL.attributedText = messageVM?.attributedLikeStr
            // 获得回复列表模型数组
            //let listModels = messageVM?.message.replaylist as! [HSReplayListModel]
            let replayListes = messageVM?.replayListAttributes ?? []
            
            if likeNum == 0 || replayListes.count == 0 {
                likeLineV.isHidden = true
            }else {
                likeLineV.isHidden = false
            }
            
            // 添加回复列表视图
            for attrbuteStr in replayListes {
                // 创建标签视图
                let l = UILabel(attributeTitle: attrbuteStr, fontSize: 12)
                l.textAlignment = .left
                addSubview(l)
                // 同时添加到记录数组中
                replayListLArray.append(l)
            }
            
            // 使用三方约束视图
            for (i,l) in replayListLArray.enumerated() {
                
                if i == 0 {
                    if likeNum == 0 {
                        l.snp.makeConstraints({ (make) in
                            make.top.equalTo(self.snp.top).offset(8)
                            make.left.equalTo(self.snp.left).offset(8)
                            make.right.equalTo(self.snp.right).offset(-8)
                        })
                    }else {
                        l.snp.makeConstraints({ (make) in
                            make.top.equalTo(likeLineV.snp.bottom).offset(5)
                            make.left.equalTo(self.snp.left).offset(8)
                            make.right.equalTo(self.snp.right).offset(-8)
                        })
                    }
                }else {
                    l.snp.makeConstraints({ (make) in
                        make.top.equalTo(replayListLArray[i-1].snp.bottom).offset(5)
                        make.left.equalTo(self.snp.left).offset(8)
                        make.right.equalTo(self.snp.right).offset(-8)
                    })
                }
            }
        }
    }
    

}






